/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ca.biosample.robot.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

/**
 *
 * @author mvs
 */
public class TubePanel extends JPanel {

    private Vector<TubeIcon> selected;
    private final int NUM_ROWS;
    private final int NUM_COLS;
    public TubeIcon tubes[];
    
    public TubePanel(int rows, int cols, String _name) {
        NUM_ROWS = rows;
        NUM_COLS = cols;
        selected = new Vector<TubeIcon>();
        //this.setPreferredSize(new Dimension(300,600));
        //tubePanel.setMinimumSize(new Dimension(200,400));
        GridLayout tpLayout = new GridLayout(NUM_ROWS,NUM_COLS);
        tpLayout.setHgap(6);
        tpLayout.setVgap(6);
        TitledBorder tpBorder = new TitledBorder(new LineBorder(Color.BLACK), _name);
        tpBorder.setTitleJustification(TitledBorder.CENTER);
        this.setBorder(tpBorder);
        this.setLayout(tpLayout);
        
        tubes = new TubeIcon[NUM_ROWS * NUM_COLS];
        for(int i = 0; i < NUM_ROWS * NUM_COLS; i++) {
            tubes[i] = new SourceTubeIcon(i);
            tubes[i].setVisible(true);
            this.add(tubes[i]);
        }
    }
    
    public boolean addSelected(TubeIcon sel) {
        if(sel.getParent() != this) {
            return false;
        }
        if(selected.contains(sel))
        	return false;
        selected.add(sel);
        return true;
    }
    
    public boolean delSelected(TubeIcon sel) {
    	if(sel.getParent() != this) {
    		return false;
    	}
    	if(!selected.contains(sel)) {
    		return false;
    	}
    	selected.remove(sel);
    	return true;
    }
    
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        for( TubeIcon sel : selected) {
            int x = sel.getX();
            int y = sel.getY();
            int w = sel.getWidth();
            int h = sel.getHeight();
            g.setColor(Color.BLUE);
            //g.drawRect(x - 3, y - 3, w + 5, h + 5);
            g.drawRect(x - 2, y - 2, w + 3, h + 3);
            g.drawRect(x - 1, y - 1, w + 1, h + 1);
            //selected.repaint();
        }
    }
}
